package com.wqiuqiu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-07-06 18:39
 **/

@Configuration
public class MyRabbitMQConfig {

    /* 容器中的Queue、Exchange、Binding 会自动创建（在RabbitMQ）不存在的情况下 */

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public Queue wareDelayQueue() {
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "ware-event-exchange");
        arguments.put("x-dead-letter-routing-key", "ware.release.ware");
        arguments.put("x-message-ttl", 120000); // 消息过期时间 2分钟 比订单释放晚一分钟，防止订单还没释放，库存就释放了
        Queue queue = new Queue("ware.delay.queue", true, false, false, arguments);

        return queue;
    }

    /**
     * 普通队列
     *
     * @return
     */
    @Bean
    public Queue wareReleaseQueue() {

        Queue queue = new Queue("ware.release.ware.queue", true, false, false);

        return queue;
    }

    /**
     * TopicExchange
     *
     * @return
     */
    @Bean
    public Exchange wareEventExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */
        return new TopicExchange("ware-event-exchange", true, false);

    }
    @Bean
    public Binding wareLockBinding() {
        return new Binding("ware.delay.queue",
                Binding.DestinationType.QUEUE,
                "ware-event-exchange",
                "ware.lock.ware",
                null);
    }
    @Bean
    public Binding wareReleaseBinding() {

        return new Binding("ware.release.ware.queue",
                Binding.DestinationType.QUEUE,
                "ware-event-exchange",
                "ware.release.ware",
                null);
    }

}
